#pragma once

using namespace System;
using namespace System::Collections;
using namespace System::ServiceProcess;
using namespace System::ComponentModel;

#include "WebServer.hh"

namespace NetService
{
	/// <summary> 
	/// Summary for NetServiceWinService
	/// </summary>
	///
	/// WARNING: If you change the name of this class, you will need to change the 
	///          'Resource File Name' property for the managed resource compiler tool 
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	public __gc class NetServiceWinService : public System::ServiceProcess::ServiceBase 
	{
		private:
        MyWebServer *webServer;
		Threading::Thread* servicethread;
	public:
		NetServiceWinService()
		{
			InitializeComponent();    
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		void Dispose(bool disposing)
		{
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
		
	protected:
		/// <summary>
		/// Set things in motion so your service can do its work.
		/// </summary>
		void OnStart(String* args[])
		{
			// TODO: Add code here to start your service.
            webServer = new MyWebServer;
			Threading::ThreadStart* threadStart = 
               new Threading::ThreadStart(this,mainLoop);
			servicethread = new Threading::Thread(threadStart);
			servicethread->Start();
		}
		void mainLoop()
		{
			dbg_print("Starting Loop");
            webServer->evt_loop();
		}
		/// <summary>
		/// Stop this service.
		/// </summary>
		void OnStop()
		{     
			// TODO: Add code here to perform any tear-down necessary to stop your service.
			dbg_print("Stop");
			servicethread->Suspend();
			delete webServer;
		}


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container *components;

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>	
		void InitializeComponent(void)
		{
			// 
			// NetServiceWinService
			// 
			this->CanPauseAndContinue = true;
			this->CanShutdown = true;
			this->ServiceName = S"RelayService";

		}		
	

	};
}
